/**
 * Copyright 2010 Andreas Keefer
 *
 * This file is part of jHashMe.
 *
 * jHashMe is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jHashMe is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with jHashMe.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.jhashme.hash.service.provider.internal;

import de.jhashme.hash.service.HashAlgorithm;
import de.jhashme.hash.service.HashServiceProvider;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


public class Sha1HashServiceProvider implements HashServiceProvider {

    private static final HashAlgorithm INFO = new HashAlgorithm("SHA-1"
            , "Secure Hash Algorithm 160-Bit");

    public HashAlgorithm getHashAlgorithm() {
        return INFO;
    }

    public String calculateHash(String textToHash) {
        Assert.assertTextToHash(textToHash);
        try {
            return MessageDigestHashCalculator.calculateHash(new ByteArrayInputStream(textToHash.getBytes()), INFO.getName());
        } catch (IOException e) {
            // this is impossible
            throw new RuntimeException(e);
        }
    }

    public String calculateHash(File fileToHash) throws IOException {
        Assert.assertFile(fileToHash);
        return MessageDigestHashCalculator.calculateHash(new FileInputStream(fileToHash), INFO.getName());
    }
}
